/**
 * 文章管理初始化
 */
var ItemDoc = {
    id: "ItemDocTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    itemid: 0,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
ItemDoc.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
            {title: '主键id', field: 'id', visible: false, align: 'center', valign: 'middle'},
            {title: '标题', field: 'title', visible: true, align: 'center', valign: 'middle'},
            {title: '图片', field: 'photo', visible: true, align: 'center', valign: 'middle',
                formatter: function(value){
                    return '<img width="100%" height="50px" src="'+value+'" class="img-rounded" >';
                }
            },
            {title: '内容', field: 'id',visible: true, align: 'center', valign: 'middle',
                formatter:function (value, row){
                    return "<a>预览</a>";
                }
            },
            {title: '栏目', field: 'itemName', visible: true, align: 'center', valign: 'middle'},
            {title: '排序号', field: 'num', visible: true, align: 'center', valign: 'middle'},
            {title: '状态', field: 'statusTag', visible: true, align: 'center', valign: 'middle'},
            {title: '点击量', field: 'pv', visible: true, align: 'center', valign: 'middle'},
            {title: '访问量', field: 'uv', visible: true, align: 'center', valign: 'middle'},
            {title: '作者', field: 'userName', visible: true, align: 'center', valign: 'middle'},
            {title: '创建时间', field: 'createtime', visible: true, align: 'center', valign: 'middle'},
            {title: '更新时间', field: 'updatetime', visible: true, align: 'center', valign: 'middle'}
    ];
};

/**
 * 检查是否选中
 */
ItemDoc.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        ItemDoc.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加文章
 */
ItemDoc.openAddItemDoc = function () {
    var index = layer.open({
        type: 2,
        title: '添加文章',
        area: ['950px', '750px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/itemDoc/itemDoc_add/'+ItemDoc.itemid
    });
    this.layerIndex = index;
};

/**
 * 打开查看文章详情
 */
ItemDoc.openItemDocDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '文章详情',
            area: ['950px', '750px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/itemDoc/itemDoc_update/' + ItemDoc.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除文章
 */
ItemDoc.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/itemDoc/delete", function (data) {
            Feng.success("删除成功!");
            ItemDoc.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("itemDocId",this.seItem.id);
        ajax.start();
    }
};

/**
 * 查询文章列表
 */
ItemDoc.search = function () {
    var queryData = {};
    queryData['itemid'] = ItemDoc.itemid;
    queryData['title'] = $("#title").val();
    ItemDoc.table.refresh({query: queryData});
};

ItemDoc.onClickDept = function (e, treeId, treeNode) {
    ItemDoc.itemid = treeNode.id;
    ItemDoc.search();
};

$(function () {
    var defaultColunms = ItemDoc.initColumn();
    var table = new BSTable(ItemDoc.id, "/itemDoc/list", defaultColunms);
    table.setPaginationType("client");
    ItemDoc.table = table.init();
    var ztree = new $ZTree("itemTree", "/item/tree");
    ztree.bindOnClick(ItemDoc.onClickDept);
    ztree.init();
});
